﻿// See https://aka.ms/new-console-template for more information
using System;

namespace AlarmClock
{
    public class Clock
    {
        public event EventHandler Tick;
        public event EventHandler Alarm;

        private DateTime time;
        private bool alarmEnabled;

        public Clock(DateTime time)
        {
            this.time = time;
            alarmEnabled = false;
        }

        public void Start()
        {
            while (true)
            {
                if (DateTime.Now >= time)
                {
                    if (alarmEnabled)
                    {
                        Console.WriteLine("闹钟响铃！");
                        Alarm?.Invoke(this, EventArgs.Empty);
                        break;
                    }
                    else
                    {
                        Console.WriteLine("闹钟时间到了，但是闹钟没有开启。");
                        break;
                    }
                }
                else
                {
                    Console.WriteLine("嘀嗒。");
                    Tick?.Invoke(this, EventArgs.Empty);
                    System.Threading.Thread.Sleep(1000);
                }
            }
        }

        public void EnableAlarm()
        {
            alarmEnabled = true;
        }

        public void DisableAlarm()
        {
            alarmEnabled = false;
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            
            DateTime alarmTime = new DateTime(2023, 3, 24, 21, 36, 40); 
            Clock clock = new Clock(alarmTime);
            clock.EnableAlarm();
            clock.Tick += (sender, e) => Console.WriteLine("现在的时间是：" + DateTime.Now);
            clock.Alarm += (sender, e) => Console.WriteLine("闹钟时间到了！");
            clock.Start();
            Console.ReadLine();

        }
    }
}

